
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:reabampos/src/reabam/operation/rb_api.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
选择导购列表
 */
class PageStateSelfServiceSelectStaffList extends XPageListViewState{

  String? sword;

  Map<String,dynamic>? currentStaff;

  PageStateSelfServiceSelectStaffList();

  @override
  void onStart() {
    setXTopBarWidget(
      height: null,
      child: RBWidgets_SelfService.getTitleBarWidget(
          this,
          isShowSearchBar: true,
          initSearchKey: sword,
          hintSearchWord: '搜索服务导购工号/姓名',
          onChangedSearchBar: (res){
            sword=res;
          },
          onSubmitSearchBar: (res){
            updateData();
          }
      ),
    );
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {

    showLoading();
    RBBaseApi.getStaffList(
        pageIndex: pageIndex,
        sword: sword,
        succeed: (response, Map<String, dynamic> responseMap, Map<String, dynamic> headers_response) {
          hideLoading();
          currentStaff=responseMap['currentStaff'];
          setXListData_pageIndexData(responseMap['PageIndex'], responseMap['PageCount'], responseMap['DataLine']);

        },
        failed: (String code, String msg) {
          hideLoading();
          toast(msg);
        }
    );
  }

  @override
  Widget getListItemWidget(listItem, int position) {

    String imgName=listItem['staffName']??'';
    if(imgName.length>2){
      imgName=imgName.substring(0,2);
    }

    bool isSelect=false;
    if(currentStaff!=null && currentStaff!['staffId']==listItem['staffId']){
      isSelect=true;
    }

    return XWidgetUtils.getWidgetGestureDetector(
      child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
          radius_bottomLeft: 16,
          radius_bottomRight: 16,
          radius_topRight: 16,
          radius_topLeft: 16,
          borderColor: isSelect?RBColors.reabamColor4:Colors.white,
          borderWidth: 3,
          alignment: Alignment.centerLeft,
          padding: XWidgetUtils.getEdgeInsets(30, 0, 30, 0),
          margin: XWidgetUtils.getEdgeInsets(0, 10, 0, 10),
          height: 220,
          child: XWidgetUtils.getLayoutRow(
              children: [
                XWidgetUtils.getLayoutContainer(
                    width:146,
                    height: 146,
                    margin: XWidgetUtils.getEdgeInsets(0, 0, 34, 0),
                    child:XWidgetUtils.getLayoutStack(
                        alignment:AlignmentDirectional.center,
                        children: [
                          XWidgetUtils.getXLayoutCircle(
                            width: 146,
                            height: 146,
                            backgroundColor: RBColors.reabamColor4,
                          ),
                          XWidgetUtils.getWidgetText(
                              imgName,
                              textAlign: TextAlign.center,
                              style: XStyleUtils.getTextStyle(
                                fontSize:30,
                                color: Colors.white,
                                lineHeight: 1,
                              )
                          )
                        ]
                    )
                ),
                XWidgetUtils.getLayoutColumn(
                    shrinkWrap:true,
                    mainAxisAlignment:MainAxisAlignment.center,
                    children: [
                      XWidgetUtils.getLayoutRow(
                          children: [
                            XWidgetUtils.getWidgetText(
                                '${listItem['staffName']??''}',
                                style: XStyleUtils.getTextStyle(
                                    fontSize:34,
                                    color: XColorUtils.color333333,
                                    fontWeight: FontWeight.bold
                                ),
                                padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0)
                            ),
                            // XStringUtils.isEmpty(listItem['staffCode'])?
                            // XWidgetUtils.getLayoutContainer():
                            // XWidgetUtils.getWidgetText(
                            //     '${listItem['staffCode']??''}',
                            //     style: XStyleUtils.getTextStyle(
                            //         fontSize:34,
                            //         color: XColorUtils.color333333,
                            //     ),
                            //   padding: XWidgetUtils.getEdgeInsets(0, 0, 10, 0)
                            // ),
                            listItem['sex']=='男'?
                            XImageUtils.getImageWidgetWithAsset(
                                'ss_nan.png',
                                width: 30,
                                height: 30
                            ):
                            XImageUtils.getImageWidgetWithAsset(
                                'ss_nv.png',
                                width: 30,
                                height: 30
                            )
                          ]
                      ),
                      XWidgetUtils.getWidgetText(
                          '${listItem['userCode']??''}',
                          style: XStyleUtils.getTextStyle(
                              fontSize:28,
                              color: XColorUtils.color999999
                          )
                      )
                    ]
                ),
                const Spacer(),
                isSelect?XImageUtils.getImageWidgetWithAsset(
                    'ss_select_1.png',
                    width:46,
                    height: 46
                ):XWidgetUtils.getLayoutContainer()
              ]
          )
      ),
      onTap: (){
        _clickItem(listItem);
      }
    );
  }


  void _clickItem(Map<String,dynamic> item){
    showLoading();
    RBBaseApi.updateStaff(
        staffId: item['staffId'],
        succeed: (response, Map<String, dynamic> responseMap, Map<String, dynamic> headers_response) {
          hideLoading();
          XSharePreferencesUtils.saveString(RBConfigs.SP_staff, XJsonUtils.obj2Json(item));
          finishPage(result: 'ok');
        },
        failed: (String code, String msg) {
          hideLoading();
          toast(msg);

        }
        );

  }

  @override
  bool getIsEnablePageLoadData() {
    return true;
  }

  @override
  EdgeInsets getListViewPadding() {
    return XWidgetUtils.getEdgeInsets(30, 10, 30, 10);
  }



}